<!doctype html>
<html><head> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
        <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="0" />
<title>HTML5 Preview Panel</title>
<script src="jquery-1.11.1.min.js"></script>

<style>@import url(http://fonts.useso.com/css?family=Raleway:400,500,700);
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section,
summary {
    display: block;
}
audio,
canvas,
video {
    display: inline-block;
}
audio:not([controls]) {
    display: none;
    height: 0;
}
[hidden] {
    display: none;
}
html {
    font-family: sans-serif;
    -ms-text-size-adjust: 100%;
    -webkit-text-size-adjust: 100%;
}
body {
    margin: 0;
	padding:0;
}
a:focus {
    outline: thin dotted;
}
a:active,
a:hover {
    outline: 0;
}
h1 {
    font-size: 2em;
    margin: 0.67em 0;
}
abbr[title] {
    border-bottom: 1px dotted;
}
b,
strong {
    font-weight: bold;
}
dfn {
    font-style: italic;
}
hr {
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    height: 0;
}
mark {
    background: #ff0;
    color: #000;
}
code,
kbd,
pre,
samp {
    font-family: monospace, serif;
    font-size: 1em;
}
pre {
    white-space: pre-wrap;
}
q {
    quotes: "\201C" "\201D" "\2018" "\2019";
}
small {
    font-size: 80%;
}
sub,
sup {
    font-size: 75%;
    line-height: 0;
    position: relative;
    vertical-align: baseline;
}
sup {
    top: -0.5em;
}
sub {
    bottom: -0.25em;
}
img {
    border: 0;
}
svg:not(:root) {
    overflow: hidden;
}
figure {
    margin: 0;
}
fieldset {
    border: 1px solid #c0c0c0;
    margin: 0 2px;
    padding: 0.35em 0.625em 0.75em;
}
legend {
    border: 0;
    padding: 0;
}
button,
input,
select,
textarea {
    font-family: inherit;
    font-size: 100%;
    margin: 0;
}
button,
input {
    line-height: normal;
}
button,
select {
    text-transform: none;
}
button,
html input[type="button"],
input[type="reset"],
input[type="submit"] {
    -webkit-appearance: button;
    cursor: pointer;
}
button[disabled],
html input[disabled] {
    cursor: default;
}
input[type="checkbox"],
input[type="radio"] {
    box-sizing: border-box;
    padding: 0;
}
input[type="search"] {
    -webkit-appearance: textfield;
    -moz-box-sizing: content-box;
    -webkit-box-sizing: content-box;
    box-sizing: content-box;
}
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
    -webkit-appearance: none;
}
button::-moz-focus-inner,
input::-moz-focus-inner {
    border: 0;
    padding: 0;
}
textarea {
    overflow: auto;
    vertical-align: top;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
@font-face {
    font-weight: normal;
    font-style: normal;
    font-family: 'codropsicons';
    src: url('../fonts/codropsicons/codropsicons.eot');
    src: url('../fonts/codropsicons/codropsicons.eot?#iefix') format('embedded-opentype'), url('../fonts/codropsicons/codropsicons.woff') format('woff'), url('../fonts/codropsicons/codropsicons.ttf') format('truetype'), url('../fonts/codropsicons/codropsicons.svg#codropsicons') format('svg');
}
*,
*:after,
*:before {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
.clearfix:before,
.clearfix:after {
    content: '';
    display: table;
}
.clearfix:after {
    clear: both;
}
body {
    background: #88abc2;
    color: #ebf7f8;
    font-weight: 500;
    font-size: 1em;
    font-family: 'Raleway', Arial, sans-serif;
}
a {
    color: #d0e0eb;
    text-decoration: none;
    outline: none;
}
a:hover,
a:focus {
    color: #fff;
}
.content {
    /*max-width: 1290px;*/
	width:100%;

    text-align: center;
}
.component {
	width:100%;
    position: relative;
    /*background: url(http://tympanus.net/Tutorials/ImageResizeCropCanvas/img/gridme.png) repeat center center;*/
    padding: 0em;
   /* height: 500px;*/
    border: 0px solid #49708a;
    /* Header */
    
    /*max-width: 901px;*/
    overflow: hidden;
   /* margin: 0 auto;*/
}
.codrops-header {
    padding: 0 0 2em;
    letter-spacing: -1px;
}
.codrops-header h1 {
    font-weight: 800;
    font-size: 2.5em;
    line-height: 1.3;
    margin: 0.25em auto;
}
.codrops-header h1 span,
.a-tip {
    color: #49708a;
}
.a-tip {
    padding: 1em;
}
.a-tip span {
    border: 2px solid #fff;
    /* Top Navigation Style */
    
    color: #fff;
    display: inline-block;
    padding: 0 5px;
}
.codrops-top {
    width: 100%;
    text-transform: uppercase;
    font-weight: 700;
    font-size: 0.69em;
    line-height: 2.2;
}
.codrops-top a {
    display: inline-block;
    padding: 1em 2em;
    text-decoration: none;
    letter-spacing: 1px;
}
.codrops-top span.right {
    float: right;
}
.codrops-top span.right a {
    display: block;
    float: left;
}
.codrops-icon:before {
    margin: 0 4px;
    text-transform: none;
    font-weight: normal;
    font-style: normal;
    font-variant: normal;
    font-family: 'codropsicons';
    line-height: 1;
    speak: none;
    -webkit-font-smoothing: antialiased;
}
.codrops-icon-drop:before {
    content: "\e001";
}
.codrops-icon-prev:before {
    content: "\e004";
}
.codrops-demos a {
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    padding: 0.5em 1em;
    margin: 5px;
    display: inline-block;
    border: 1px solid black;
    border-color: initial;
    border-radius: 4px;
}
.codrops-demos a.current-demo {
    color: #74777b;
}
.related {
    text-align: center;
    padding: 4em 0 2em;
}
.related > a {
    width: calc(100% - 20px);
    max-width: 340px;
    border: 2px solid #49708a;
    display: inline-block;
    text-align: center;
    vertical-align: top;
    margin: 20px 10px;
    padding: 25px;
}
.related a img {
    max-width: 100%;
    opacity: 0.8;
}
.related a:hover img,
.related a:active img {
    opacity: 1;
}
.related a h3 {
    margin: 0;
    padding: 0.5em 0 0.3em;
    /* Demo ad design */
    
    max-width: 300px;
    text-align: left;
    min-height: 60px;
}
body #cdawrap {
    background: rgba(0, 0, 0, 0.05);
    top: 50px;
    border: none;
}
body #cdawrap a {
    color: #fff !important;
}
body #cda-remove {
    color: #fff;
}
@media screen and (max-width: 27em) {
    .codrops-icon {
        font-size: 1.5em;
    }
    .codrops-icon span {
        display: none;
    }
}
.resize-container {
    position: relative;
    display: inline-block;
    cursor: move;
    margin: 0 auto;
}
.resize-container img {
    display: block;
}
.resize-container:hover img,
.resize-container:active img {
    outline: 2px dashed rgba(222, 60, 80, .9);
}
.resize-handle-ne,
.resize-handle-se,
.resize-handle-nw,
.resize-handle-sw {
    position: absolute;
    display: block;
    width: 10px;
    height: 10px;
    background: rgba(222, 60, 80, .9);
    z-index: 999;
}
.resize-handle-nw {
    top: -5px;
    left: -5px;
    cursor: nw-resize;
}
.resize-handle-sw {
    bottom: -5px;
    left: -5px;
    cursor: sw-resize;
}
.resize-handle-ne {
    top: -5px;
    right: -5px;
    cursor: ne-resize;
}
.resize-handle-se {
    bottom: -5px;
    right: -5px;
    cursor: se-resize;
}
.overlay {
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -100px;
    margin-top: -100px;
    z-index: 999;
    width: 200px;
    height: 200px;
    border: solid 2px rgba(222, 60, 80, .9);
    box-sizing: content-box;
    pointer-events: none;
}
.overlay:after,
.overlay:before {
    content: '';
    position: absolute;
    display: block;
    width: 204px;
    height: 40px;
    border-left: dashed 2px rgba(222, 60, 80, .9);
    border-right: dashed 2px rgba(222, 60, 80, .9);
}
.overlay:before {
    top: 0;
    margin-left: -2px;
    margin-top: -40px;
}
.overlay:after {
    bottom: 0;
    margin-left: -2px;
    margin-bottom: -40px;
}
.overlay-inner:after,
.overlay-inner:before {
    content: '';
    position: absolute;
    display: block;
    width: 40px;
    height: 204px;
    border-top: dashed 2px rgba(222, 60, 80, .9);
    border-bottom: dashed 2px rgba(222, 60, 80, .9);
}
.overlay-inner:before {
    left: 0;
    margin-left: -40px;
    margin-top: -2px;
}
.overlay-inner:after {
    right: 0;
    margin-right: -40px;
    margin-top: -2px;
}

.button{
	
	position:fixed;
	bottom:0;
	left:0;
	padding:10px 0px;
	width:100%;
	z-index:9;
	background:rgba(0,0,0,.7);
	
	}
.button a{
	color:#fff;
	margin:0 10px;
	}

</style></head><body>
        <div class="component">
            <div class="overlay">
                <div class="overlay-inner">
                </div>
            </div>
            <div class="resize-container"><span class="resize-handle resize-handle-nw"></span><span class="resize-handle resize-handle-ne"></span><img class="resize-image" src="beauty.jpg" alt="image for resizing"><span class="resize-handle resize-handle-sw"></span><span class="resize-handle resize-handle-se"></span></div>
            <div class="button">
            <a class="btn-cancel" style="float:left">取消</a>
            <a class="btn-crop js-crop" style="float:right">确定</a>
            </div>
        </div>

<script>

var resizeableImage = function(image_target) {
    // Some variable and settings
    var $container,
        orig_src = new Image(),
		img = image_target,
        image_target = $(image_target).get(0),
        event_state = {},
        constrain = true,
        min_width = 60, // Change as required
        min_height = 60,
        max_width = 800, // Change as required
        max_height = 900,
        resize_canvas = document.createElement('canvas');
	var ismove = true;

    init = function() {

        // When resizing, we will always use this copy of the original as the base
        orig_src.src = image_target.src;
        $('.component').height($(window).height());
		$(image_target).width($(window).width());
		
		var img_w = $(image_target).width();
		var img_h = $(image_target).height();
		var canvas = document.createElement('canvas');
        canvas.width = img_w;
        canvas.height = img_h;
        
        canvas.getContext('2d').drawImage(image_target, 0, 0, img_w, img_h);
		var src = canvas.toDataURL("image/png");
		image_target.src = src
		orig_src.src = src;
		
		
        // Wrap the image with the container and add resize handles
        $(image_target).wrap('<div class="resize-container"></div>')
            .before('<span class="resize-handle resize-handle-nw"></span>')
            .before('<span class="resize-handle resize-handle-ne"></span>')
            .after('<span class="resize-handle resize-handle-se"></span>')
            .after('<span class="resize-handle resize-handle-sw"></span>');

        // Assign the container to a variable
        $container = $(image_target).parent('.resize-container');

        // Add events
        $container.on('mousedown touchstart', '.resize-handle', startResize);
		//$container.on('mouseup touchend', '.resize-handle', endResize);
		
        $container.on('mousedown touchstart', 'img', startMoving);
        $('.js-crop').on('click', crop);
		$('.btn-zoomin').on('click', zoomin);
		$('.btn-zoomout').on('click', zoomout);
    };
    
	zoomin = function(e){
		var w = $(image_target).width() * 1.2;
		$(image_target).width(w);
	};
	
	zoomout = function(e){
		var w = $(image_target).width() / 1.2;
		if (w < $(window).width()) return false;
		$(image_target).width(w);
	};
    startResize = function(e) {
        e.preventDefault();

        e.stopPropagation();
        saveEventState(e);
        $(document).on('mousemove touchmove', resizing);
        $(document).on('mouseup touchend', endResize);
    };

    endResize = function(e) {
        e.preventDefault();
		ismove = true;
        $(document).off('mouseup touchend', endResize);
        $(document).off('mousemove touchmove', resizing);
    };

    saveEventState = function(e) {
		ismove = false;
        // Save the initial event details and container state
        event_state.container_width = $container.width();
        event_state.container_height = $container.height();
        event_state.container_left = $container.offset().left;
        event_state.container_top = $container.offset().top;
        event_state.mouse_x = (e.clientX || e.pageX || e.originalEvent.touches[0].clientX) + $(window).scrollLeft();
        event_state.mouse_y = (e.clientY || e.pageY || e.originalEvent.touches[0].clientY) + $(window).scrollTop();

        // This is a fix for mobile safari
        // For some reason it does not allow a direct copy of the touches property
        if (typeof e.originalEvent.touches !== 'undefined') {
            event_state.touches = [];
            $.each(e.originalEvent.touches, function(i, ob) {
                event_state.touches[i] = {};
                event_state.touches[i].clientX = 0 + ob.clientX;
                event_state.touches[i].clientY = 0 + ob.clientY;
            });
        }
        event_state.evnt = e;
    };

    resizing = function(e) {
		//if (!ismove) return false;
        var mouse = {},
            width, height, left, top, offset = $container.offset();
        mouse.x = (e.clientX || e.pageX || e.originalEvent.touches[0].clientX) + $(window).scrollLeft();
        mouse.y = (e.clientY || e.pageY || e.originalEvent.touches[0].clientY) + $(window).scrollTop();

        // Position image differently depending on the corner dragged and constraints
        if ($(event_state.evnt.target).hasClass('resize-handle-se')) {
            width = mouse.x - event_state.container_left;
            height = mouse.y - event_state.container_top;
            left = event_state.container_left;
            top = event_state.container_top;
        } else if ($(event_state.evnt.target).hasClass('resize-handle-sw')) {
            width = event_state.container_width - (mouse.x - event_state.container_left);
            height = mouse.y - event_state.container_top;
            left = mouse.x;
            top = event_state.container_top;
        } else if ($(event_state.evnt.target).hasClass('resize-handle-nw')) {
            width = event_state.container_width - (mouse.x - event_state.container_left);
            height = event_state.container_height - (mouse.y - event_state.container_top);
            left = mouse.x;
            top = mouse.y;
            if (constrain || e.shiftKey) {
                top = mouse.y - ((width / orig_src.width * orig_src.height) - height);
            }
        } else if ($(event_state.evnt.target).hasClass('resize-handle-ne')) {
            width = mouse.x - event_state.container_left;
            height = event_state.container_height - (mouse.y - event_state.container_top);
            left = event_state.container_left;
            top = mouse.y;
            if (constrain || e.shiftKey) {
                top = mouse.y - ((width / orig_src.width * orig_src.height) - height);
            }
        }

        // Optionally maintain aspect ratio
        if (constrain || e.shiftKey) {
            height = width / orig_src.width * orig_src.height;
        }

        if (width > min_width && height > min_height && width < max_width && height < max_height) {
            // To improve performance you might limit how often resizeImage() is called
            resizeImage(width, height);
            // Without this Firefox will not re-calculate the the image dimensions until drag end
            $container.offset({
                'left': left,
                'top': top
            });
        }
    }

    resizeImage = function(width, height) {
		if (width < $('.overlay').width()) return false;
		resize_canvas = document.createElement('canvas');
        resize_canvas.width = width;
        resize_canvas.height = height;
		var c = resize_canvas.getContext('2d');
		var scale = (width/orig_src.width).toFixed(2);
		
        //resize_canvas.getContext('2d').drawImage(orig_src, 0, 0, width, height);
		//c.scale(scale, scale);//缩放
		c.drawImage(orig_src, 0, 0, width, height);
        $(image_target).width(width).height(height).attr({'src': resize_canvas.toDataURL("image/png")});
		resize_canvas = null;
    };

    startMoving = function(e) {
        e.preventDefault();
        e.stopPropagation();
        saveEventState(e);
        $(document).on('mousemove touchmove', moving);
        $(document).on('mouseup touchend', endMoving);
    };

    endMoving = function(e) {
        e.preventDefault();
        $(document).off('mouseup touchend', endMoving);
        $(document).off('mousemove touchmove', moving);
    };

    moving = function(e) {
        var mouse = {},
            touches;
        e.preventDefault();
        e.stopPropagation();

        touches = e.originalEvent.touches;

        mouse.x = (e.clientX || e.pageX || touches[0].clientX) + $(window).scrollLeft();
        mouse.y = (e.clientY || e.pageY || touches[0].clientY) + $(window).scrollTop();
        $container.offset({
            'left': mouse.x - (event_state.mouse_x - event_state.container_left),
            'top': mouse.y - (event_state.mouse_y - event_state.container_top)
        });
		//alert(event_state.touches.length+':'+ touches.length);
        // Watch for pinch zoom gesture while moving
        if (event_state.touches && event_state.touches.length > 1 && touches.length > 1) {
            var width = event_state.container_width,
                height = event_state.container_height;
            var a = event_state.touches[0].clientX - event_state.touches[1].clientX;
            a = a * a;
            var b = event_state.touches[0].clientY - event_state.touches[1].clientY;
            b = b * b;
            var dist1 = Math.sqrt(a + b);

            a = e.originalEvent.touches[0].clientX - touches[1].clientX;
            a = a * a;
            b = e.originalEvent.touches[0].clientY - touches[1].clientY;
            b = b * b;
            var dist2 = Math.sqrt(a + b);

            var ratio = dist2 / dist1;

            width = width * ratio;
            height = height * ratio;
			//alert(width+':'+ height)
            // To improve performance you might limit how often resizeImage() is called
            resizeImage(width, height);
        }
    };
	rotate = function(src, direction){
    //最小与最大旋转方向，图片旋转4次后回到原方向  
        var min_step = 0;  
        var max_step = 3;  
        var img = new Image();
		img.src = src;  
        if (img == null)return;  
        //img的高度和宽度不能在img元素隐藏后获取，否则会出错  
        var height = img.height;  
        var width = img.width;  
        var step = img.getAttribute('step');  
        if (step == null) {  
            step = min_step;  
        }  
        if (direction == 'right') {  
            step++;  
            //旋转到原位置，即超过最大值  
            step > max_step && (step = min_step);  
        } else {  
            step--;  
            step < min_step && (step = max_step);  
        }  
        img.setAttribute('step', step);  
        var canvas = document.createElement('canvas'); 
       
        //旋转角度以弧度值为参数  
        var degree = step * 90 * Math.PI / 180;  
        var ctx = canvas.getContext('2d');  
        switch (step) {  
            case 0:  
                canvas.width = width;  
                canvas.height = height;  
                ctx.drawImage(img, 0, 0);  
                break;  
            case 1:  
                canvas.width = height;  
                canvas.height = width;  
                ctx.rotate(degree);  
                ctx.drawImage(img, 0, -height);  
                break;  
            case 2:  
                canvas.width = width;  
                canvas.height = height;  
                ctx.rotate(degree);  
                ctx.drawImage(img, -width, -height);  
                break;  
            case 3:  
                canvas.width = height;  
                canvas.height = width;  
                ctx.rotate(degree);  
                ctx.drawImage(img, -width, 0);  
                break;  
        }
		return canvas;
		callback && callback(canvas);
	};
    crop = function() {
        //Find the part of the image that is inside the crop box
		var img_w = $(image_target).width();
		var scale = img_w/$(window).width();
		var scale_wh = scale < 1 ? scale : 1;
		//alert(scale);
        var crop_canvas,
            left = $('.overlay').offset().left - $container.offset().left,
            top = $('.overlay').offset().top - $container.offset().top,
            width = $('.overlay').width(),
            height = $('.overlay').height();
        //alert(left+':'+top+'|'+width+':'+width);
        crop_canvas = document.createElement('canvas');
        crop_canvas.width = width;
        crop_canvas.height = height;
       // var image  = $('.resize-image').get(0);
		//alert(image.width+''+image.height)
		//image.src = image_target.src;
		var ctx = crop_canvas.getContext("2d");
		//alert(scale)
		if (scale < 1){
            //ctx.scale(scale,scale);//缩放
		}else{
			//ctx.scale(scale,scale);//缩放
		}
		var img = $('.resize-image').get(0);
        var image_new = new Image();
		image_new.src = img.src;
		image_new.width = img.width;
		image_new.height = img.height;
		//alert(image_target.width);
        ctx.drawImage(image_new, left, top, width, height, 0, 0, width, height);
		var src = crop_canvas.toDataURL("image/png");
		//如果放大或缩小，还原大小
		if (scale != 1){
			/*width = width * scale;
			height = height * scale;
			alert(width+':'+height)
			var img = new Image(width, height);
			img.src = src;

			
			var canvas = document.createElement('canvas');
			canvas.width = width;
			canvas.height = height;
			//rotate(src, 'right');
			var ctx = canvas.getContext("2d");
			
			ctx.drawImage(img, 0, 0, width, height);
			src = canvas.toDataURL("image/png");
			*/
		}
		
        window.open(src);
    }

    init();
};

// Kick everything off with the target image
$(function(){
	resizeableImage($('.resize-image'));
});
</script>
</body></html>